﻿//-----------------------------------------------------------------------
// <copyright file="App.xaml.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace vsarFlightPlan
{
    using System;
    using System.Collections.Generic;
    using Windows.ApplicationModel.Activation;
    using Windows.ApplicationModel.Background;
    using Windows.ApplicationModel.Resources;
    using Windows.System;
    using Windows.UI.ApplicationSettings;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Navigation;

    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    sealed partial class App : Application
    {
        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            this.InitializeComponent();
        }

        public Frame RootFrame { get; set; }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected async override void OnLaunched(LaunchActivatedEventArgs e)
        {
            this.RootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (RootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                RootFrame = new Frame();
                // Set the default language
                RootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];

                RootFrame.NavigationFailed += OnNavigationFailed;

                // Place the frame in the current Window
                Window.Current.Content = RootFrame;
            }

            if (RootFrame.Content == null)
            {
                RootFrame.Navigate(typeof(MainPage));
            }

            // Ensure the current window is active
            Window.Current.Activate();
            await BackgroundTasks.Utilities.RegisterTask("updater", "vsarFlightPlanSupport.BackgroundTask",
               new List<IBackgroundTrigger>() { new TimeTrigger(60, false) },
               new List<IBackgroundCondition>() { new SystemCondition(SystemConditionType.InternetAvailable) });
            SettingsPane.GetForCurrentView().CommandsRequested += App_CommandsRequested;
        }

        /// <summary>
        /// App_s the commands requested.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The <see cref="SettingsPaneCommandsRequestedEventArgs"/> instance containing the event data.</param>
        private void App_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
        {
            var resourceLoader = ResourceLoader.GetForCurrentView("LocalisedStrings");
            args.Request.ApplicationCommands.Add(
                new SettingsCommand(
                    "settings",
                    "Settings",
                    c =>
                    {
                        RootFrame.Navigate(typeof(Settings));
                    }));

            args.Request.ApplicationCommands.Add(
                new SettingsCommand(
                    "sendfeedback",
                    "Provide Feedback",
                    async c =>
                    {
                        await Launcher.LaunchUriAsync(new Uri(resourceLoader.GetString("FeedbackURL")));
                    }));

            args.Request.ApplicationCommands.Add(
                new SettingsCommand(
                    "privacyPref",
                    "Privacy Policy",
                    async c =>
                    {
                        // Launch the privacy policy URL.
                        await Launcher.LaunchUriAsync(new Uri(resourceLoader.GetString("PrivacyPolicyURL")));
                    }));
        }

        /// <summary>
        /// Invoked when Navigation to a certain page fails
        /// </summary>
        /// <param name="sender">The Frame which failed navigation</param>
        /// <param name="e">Details about the navigation failure</param>
        private void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }
    }
}